home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Buttons and Labels and Scrolls (Oh, My!) / BitmapButtons / BitmapButtons.cs next >
Encoding:
Text File  |  2001-01-15  |  2.4 KB  |  76 lines

  1. //--------------------------------------------
  2. // BitmapButtons.cs ⌐ 2001 by Charles Petzold
  3. //--------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class BitmapButtons: Form
  9. {
  10.      readonly int    cxBtn, cyBtn, dxBtn;
  11.      readonly Button btnLarger, btnSmaller;
  12.  
  13.      public static void Main()
  14.      {
  15.           Application.Run(new BitmapButtons());
  16.      }
  17.      public BitmapButtons()
  18.      {
  19.           Text = "Bitmap Buttons";
  20.           ResizeRedraw = true;
  21.  
  22.           dxBtn = Font.Height;
  23.  
  24.                // Create first button.
  25.  
  26.           btnLarger = new Button();
  27.           btnLarger.Parent = this;
  28.           btnLarger.Image  = new Bitmap(GetType(), 
  29.                                    "BitmapButtons.LargerButton.bmp") ;
  30.  
  31.                // Calculate button dimensions based on image dimensions.
  32.  
  33.           cxBtn = btnLarger.Image.Width  + 8;
  34.           cyBtn = btnLarger.Image.Height + 8;
  35.  
  36.           btnLarger.Size   = new Size(cxBtn, cyBtn);
  37.           btnLarger.Click += new EventHandler(ButtonLargerOnClick);
  38.  
  39.                // Create second button.
  40.  
  41.           btnSmaller = new Button();
  42.           btnSmaller.Parent = this;
  43.           btnSmaller.Image  = new Bitmap(GetType(), 
  44.                                    "BitmapButtons.SmallerButton.bmp");
  45.           btnSmaller.Size   = new Size(cxBtn, cyBtn);
  46.           btnSmaller.Click += new EventHandler(ButtonSmallerOnClick);
  47.  
  48.          OnResize(EventArgs.Empty);
  49.      }
  50.      protected override void OnResize(EventArgs ea)
  51.      {
  52.           base.OnResize(ea);
  53.  
  54.           btnLarger.Location =
  55.                          new Point(ClientSize.Width / 2 - cxBtn - dxBtn / 2,
  56.                                   (ClientSize.Height - cyBtn) / 2);
  57.           btnSmaller.Location =
  58.                          new Point(ClientSize.Width / 2 + dxBtn / 2,
  59.                                   (ClientSize.Height - cyBtn) / 2);
  60.      }
  61.      void ButtonLargerOnClick(object obj, EventArgs ea)
  62.      {
  63.           Left   -= (int)(0.05 * Width);
  64.           Top    -= (int)(0.05 * Height);
  65.           Width  += (int)(0.10 * Width);
  66.           Height += (int)(0.10 * Height);
  67.      }
  68.      void ButtonSmallerOnClick(object obj, EventArgs ea)
  69.      {
  70.           Left   += (int)(Width  / 22f);
  71.           Top    += (int)(Height / 22f);
  72.           Width  -= (int)(Width  / 11f);
  73.           Height -= (int)(Height / 11f);
  74.      }
  75. }
  76.